package com.hzncc.scaffold.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.github.pagehelper.PageHelper;
import com.hzncc.scaffold.config.MinioConfig;
import com.hzncc.scaffold.entity.Bo.CertificatesRecordListBo;
import com.hzncc.scaffold.entity.Other.Certificate;
import com.hzncc.scaffold.entity.Vo.*;
import com.hzncc.scaffold.mapper.CertificateMapper;
import com.hzncc.scaffold.mapper.PaperrecordMapper;
import com.hzncc.scaffold.service.CertificateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 证书表 服务实现类
 * </p>
 *
 * @author DrunkBear
 * @since 2022-04-27
 */
@Service
public class CertificateServiceImpl extends ServiceImpl<CertificateMapper, Certificate> implements CertificateService {

    @Autowired
    PaperrecordMapper paperrecordMapper;

    @Autowired
    private CertificateMapper  certificateMapper;

    @Value("${project.imgFileSuffix}")
    public String imgFileSuffix;



    @Override
    public ArrayList<CertificatesRecordListVo> recordList(Integer pageNum, Integer pageSize, Integer courseId, String keyword, String startTime, String endTime, Integer type) {

        QueryWrapper<CertificatesRecordListBo> qw = new QueryWrapper<>();
        if (courseId != null) {
            qw.eq("paperrecord.courseIds", courseId);
        }
        if (StrUtil.isNotEmpty(keyword)) {
            qw.like("ownerRealname", keyword);
        }
        if (StrUtil.isNotEmpty(startTime) && StrUtil.isNotEmpty(endTime)) {
            qw.between("paperrecord.createTime", startTime, endTime);
        }
        qw.groupBy("ownerId");
        qw.orderByDesc("paperrecord.id");

        List<CertificatesRecordListBo> certificatesRecordListBos = new ArrayList<>();
        if(type == 0){
            certificatesRecordListBos = paperrecordMapper.recordList(qw);
        }

        if(type == 1){
            certificatesRecordListBos = paperrecordMapper.recordListClass(qw);
        }

        ArrayList<CertificatesRecordListVo> certificatesRecordListVos = new ArrayList<>();

        for (CertificatesRecordListBo certificatesRecordListBo : certificatesRecordListBos) {
            CertificatesRecordListVo certificatesRecordListVo = BeanUtil.toBean(certificatesRecordListBo, CertificatesRecordListVo.class);

            CertificatesVo certificatesVo = new CertificatesVo();
            certificatesVo.setId(certificatesRecordListBo.getCertificateId());
            certificatesVo.setCertNum(certificatesRecordListBo.getCertificateCertNum());
            certificatesVo.setImgUrl(imgFileSuffix +certificatesRecordListBo.getCertificateImgUrl());
            certificatesRecordListVo.setCertificate(certificatesVo);

            CertificateOwnerVo ownerVo = new CertificateOwnerVo();
            ownerVo.setId(certificatesRecordListBo.getOwnerId());
            ownerVo.setAccount(certificatesRecordListBo.getOwnerAccount());
            ownerVo.setGender(certificatesRecordListBo.getOwnerGender());
            ownerVo.setIdCardNum(certificatesRecordListBo.getOwnerIdCardNum());
            ownerVo.setInstituteName(certificatesRecordListBo.getOwnerInstituteName());
            ownerVo.setName(certificatesRecordListBo.getOwnerName());
            ownerVo.setRealname(certificatesRecordListBo.getOwnerRealname());
            ownerVo.setTelephone(certificatesRecordListBo.getOwnerTelephone());
            certificatesRecordListVo.setOwner(ownerVo);

            CertificateCourseVo certificateCourseVo = new CertificateCourseVo();
            certificateCourseVo.setId(certificatesRecordListBo.getCourseIds());
            certificateCourseVo.setCertTemplate(certificatesRecordListBo.getCertTemplate());
            certificateCourseVo.setSetHeight(certificatesRecordListBo.getSetHeight());
            certificateCourseVo.setSetWidth(certificatesRecordListBo.getSetHeight());
            certificateCourseVo.setTitle(certificatesRecordListBo.getCourseTitle());

            certificatesRecordListVo.setCertTemplate(certificatesRecordListBo.getCertTemplate());
            certificatesRecordListVo.setCourse(certificateCourseVo);

            ArrayList<CertificateChaptersVo> chapters = new ArrayList<>();
            CertificateChaptersVo certificateChaptersVo = new CertificateChaptersVo();
            certificateChaptersVo.setId(certificatesRecordListBo.getCourseChaptersId());


            ArrayList<TasksVo> tasks = new ArrayList<>();
            TasksVo tasksVo = new TasksVo();
            tasksVo.setId(certificatesRecordListBo.getCourseChaptersTasksId());
            tasksVo.setExamType(certificatesRecordListBo.getCourseChaptersTasksExamType());
            tasksVo.setTitle(certificatesRecordListBo.getCourseChaptersTasksTitle());
            tasks.add(tasksVo);

            certificateChaptersVo.setTasks(tasks);
            chapters.add(certificateChaptersVo);

            certificateCourseVo.setCertTemplate(imgFileSuffix +certificateCourseVo.getCertTemplate());
            certificateCourseVo.setChapters(chapters);

            certificatesRecordListVos.add(certificatesRecordListVo);
        }
        return certificatesRecordListVos;
    }

    @Override
    public List<Certificate> listAll( QueryWrapper queryWrapper) {
        return certificateMapper.listAll(queryWrapper);
    }


}
